import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:myflutter/config/color_config.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:lottie/lottie.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../dialog/loading_dialog_utils.dart';

/// Create by wanjin on 2021/10/21
/// 自定义通用Widget

class CommonWidgets {
  CommonWidgets._();

  ///刷新头部样式
  static Widget refresherHeader(BuildContext context) {
    LottieBuilder lottieBuilder = Lottie.asset("assets/anim_pull_refresh.json",
        width: 45, height: 45, reverse: true);
    Widget commonState =
        Image.asset("assets/images/common_pulling.png", width: 60, height: 60);
    return CustomHeader(
      builder: (context, mode) {
        Widget? body;
        if (mode == RefreshStatus.idle) {
          body = commonState;
        } else if (mode == RefreshStatus.refreshing) {
          body = lottieBuilder;
        } else if (mode == RefreshStatus.canRefresh) {
          body = lottieBuilder;
        }else if (mode == RefreshStatus.failed) {
          body = commonState;
        }else if (mode == RefreshStatus.completed) {
          body = commonState;
        }
        return Container(
          height: 60.0,
          child: Center(
            child: body,
          ),
        );
      },
    );
  }

  ///加载底部样式
  static Widget refresherFooter(BuildContext context) {
    return ClassicFooter(
      loadingText: "加载中···",
      idleText: '上拉加载',
      canLoadingText: '松开加载更多',
      failedText: "加载失败，请重试",
      noDataText: "",
      noMoreIcon: Container(
        child: Center(
          child: Text(
            "——没有更多了——",
            style: TextStyle(
              fontSize: 12,
              color: Color(0xff999999),
            ),
          ),
        ),
      ),
    );
  }



  ///统一带缓存的网络图片加载
  static Widget buildCachedNetworkImage(String? url, {String? defaultImg,
      String? erroImg,
      Color? backgroundColor,
      BoxFit? fit,
      double? width,
      double? height,
      Widget Function(BuildContext context, String url, DownloadProgress progress)? progressBuilder}) {
    if(url==null||url.isEmpty){
      if((defaultImg==null||defaultImg.isEmpty)&&(erroImg==null||erroImg.isEmpty)){
        return Container(
            width: width,
            height: height,
            color: backgroundColor??Colors.white,
        );
      }
      return Image.asset(defaultImg??erroImg??"",width: width,height: height);
    }
    //加载动画和占位图只能用一项，不能同时使用
    if(defaultImg!=null&&defaultImg.isNotEmpty){
      return CachedNetworkImage(
        fit: fit,
        imageUrl: url,
        width: width,
        height: height,
        placeholder: (context, string) {
          return Image.asset(defaultImg,width: width,height: height);
        },
        errorWidget: (context, string, a ){
          return Image.asset(erroImg!,width: width,height: height);
        },
      );
    }
    if(erroImg!=null&&erroImg.isNotEmpty){
      return CachedNetworkImage(
        fit: fit,
        imageUrl: url,
        width: width,
        height: height,
        progressIndicatorBuilder: progressBuilder,
        errorWidget: (context, string, a ){
          return Image.asset(erroImg,width: width,height: height);
        },
      );
    }
    return CachedNetworkImage(
      fit: fit,
      imageUrl: url,
      width: width,
      height: height,
      progressIndicatorBuilder: progressBuilder,
    );
  }

  ///Flutter_Boost方式页面路由跳转
  static Future jump(String routerUri, {bool? withContainer, Map<String, dynamic>? arguments}) {
    return BoostNavigator.instance.push(routerUri, withContainer: withContainer ?? false, arguments: arguments);
  }

  ///顶部导航栏
  static AppBar CustomAppBar(BuildContext context, title, {Widget? leftWidget,double elevation = 0, List<Widget>? actions, Color? backgroundColor,String? backIconAssets}) {
    return AppBar(
      backgroundColor: backgroundColor ?? ColorConfig.colorF5F6F7,
      leading: leftWidget ?? InkWell(
        onTap: () => BoostNavigator.instance.pop(),
        borderRadius: const BorderRadius.all(Radius.circular(999.0)),
        child: backIconAssets==null
            ?const Icon(Icons.arrow_back_ios_outlined, size: 20, color: ColorConfig.text333333)
            :Image.asset(backIconAssets,width: 20,height: 20),
      ),
      centerTitle: true,
      elevation: elevation,
      title: Text(
        title,
        overflow: TextOverflow.ellipsis,
        style: const TextStyle(color: ColorConfig.text333333, fontSize: 18, fontWeight: FontWeight.bold),
      ),
      actions: actions,
    );
  }

  ///无数据widget和 404报错widget
  static Widget noDataWidget({String codeStr = "SUCCESS"}) {
    Widget bodyWidget;
    bodyWidget = Container(
      margin: EdgeInsets.only(top: 150),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(codeStr == "SUCCESS"
              ? "assets/images/ic_nodata.png"
              : "assets/images/ic_404.png"),
          SizedBox(height: 5),
          Text(codeStr == "SUCCESS" ? "暂无数据" : "程序猿已被干掉",
              style: ColorConfig.text999999_12),
        ],
      ),
    );
    return bodyWidget;
  }

  ///图片、提示、内容自定义无数据widget
  static Widget customNoDataWidget({required String imgUrl, String? msg,double? height, Widget? child}) {
    Widget bodyWidget;
    bodyWidget = Container(
      // width: double.infinity,
      height: height??ScreenUtil().screenHeight/2,
      alignment: Alignment.center,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(imgUrl),
          SizedBox(height: 5),
          msg !=null&&msg.length>0 ? Text(msg, style: ColorConfig.text999999_14):Container(),
          SizedBox(height: 15),
          child??Container(),
        ],
      ),
    );
    return bodyWidget;
  }

  ///加载中展位图
  static Widget loadWidget() {
    Widget bodyWidget;
    bodyWidget = Container(
      alignment: Alignment.center,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Lottie.asset("assets/anim_loading.json",
          width: 100, height: 100, repeat: true),
          SizedBox(height: 15),
          Text('努力加载中...',
              style: ColorConfig.text999999_14),
        ],
      ),
    );
    return bodyWidget;
  }

  ///加载中二级评论自定义
  static Widget loadTwoCommentWidget(BuildContext context) {
    Widget bodyWidget;
    bodyWidget = Container(
      alignment: Alignment.center,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Lottie.asset('assets/comments_loading.json',
              width: 80, height: 30, repeat: true),
          SizedBox(height: 15),
        ],
      ),
    );
    return bodyWidget;
  }

  ///显示加载框
  static void showLoadingWidgets(BuildContext context) {
    Future.delayed(Duration.zero,(){
      LoadingDialogUtils.instance.showLoading(context);
    });
  }

  ///取消加载框
  static void dismissLoading() {
    Future.delayed(Duration.zero,(){
      LoadingDialogUtils.instance.dismiss();
    });
  }
}
